import { useGetpropertymappingQuery } from "../store/request/propertymappingrequest";
import { useState, useEffect } from 'react';
import { Radio } from 'antd';
export const PropertyHook = (array) => {
    const  categorymapping = useGetpropertymappingQuery();
    const [data, setData] = useState([]);//存储从后台请求的属性数据，从而让下面的几个方法使用这个数据
    useEffect(() => {
      //console.log("hook"+categorymapping);
      if(categorymapping.isSuccess){
        categorymapping.refetch();
        //console.log("请求成功");
        const categorytypearray = categorymapping.isSuccess? categorymapping.data?.filter(item =>array.includes(item.name)):[];
        setData(categorytypearray);
        //console.log(categorytypearray);
      }

    },[categorymapping.isSuccess]);
    
    //表格显示categorie时不是固定的，而是动态的，所以需要请求后端来获得数据，再进行处理来给前端，这里是添加或编辑数据时动态显示category
  const categorymapforradio = (n) => {
    //const categorytypearray = categorymapping.isSuccess? categorymapping.data?.filter(item => item.name === n):[];
   //console.log(data);
    const radioGroup = data?.filter(item =>item.name===n).map((option) =>
      <Radio value={option.value} key={option.key}>{option.meaning}</Radio>
    );
    return radioGroup;
  };
  const categorymapforrender = (value,n) => {
    //const categorytypearray = categorymapping.isSuccess? categorymapping.data?.filter(item => item.name === n):[];
    return data?.filter(item =>item.name===n).find((c)=>c.value===value)?.meaning;
   //   `${c ? '传感器' : '设备'}`
    
  };
  const categorymapforfilter = (n) => {
    //const categorytypearray = categorymapping.isSuccess? categorymapping.data?.filter(item => item.name === n):[];
    //console.log(categorytypearray);
    
    var newArray = data?.filter(item =>item.name===n).map(function(obj){
      return {text: obj.meaning, value: obj.value};
    });
    return newArray;
  };
  
    return {
      
      categorymapforradio,
      categorymapforrender,
      categorymapforfilter
    
    };
  };